package com.example.threadmodel;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@SpringBootTest
public class Demo1_RemoveEmptyActionQueue {
    /**
     * 测试ConcurrentHashMap中队列的移除(考虑到玩家下线等情况)
     *
     * @throws Exception
     */
    @Test
    void testQ() throws Exception {
        ConcurrentHashMap<String, String> uid2QueueMap = new ConcurrentHashMap<>();
        uid2QueueMap.put("1", "1");
        uid2QueueMap.put("2", "2");
        log.info("before size={}", uid2QueueMap.size()); // 2

        // 相当于remove掉了一个队列
        uid2QueueMap.compute("1", (k, v) -> {
            if (false) {       // 加一些条件, 比如: 多久没执行

            }
            return null;
        });

        log.info("after size={}", uid2QueueMap.size());
    }
}
